<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script>
var testCases = [["div0", "div1", "div2"],
                 ["div0", "div2", "div1"],
                 ["div1", "div0", "div2"],
                 ["div1", "div2", "div0"],
                 ["div2", "div0", "div1"],
                 ["div2", "div1", "div0"]];

var rootDiv = document.createElement("div");
document.body.appendChild(rootDiv);
var testHtml = "<div id='div0-parent'><div id='div0'><div id='div0-child'></div></div><div id='div1-parent'><div id='div1'><div id='div1-child'></div></div><div id='div2-parent'><div id='div2'><div id='div2-child'></div></div></div></div></div>";

testCases.forEach(function (test) {
    var divX, divY, divZ;
    debug("=== Initial state ===");
    rootDiv.innerHTML = testHtml;
    divX = document.getElementById(test[0]);
    divY = document.getElementById(test[1]);
    divZ = document.getElementById(test[2]);
    checkParentAndChildAlive(divX, test[0]);
    checkParentAndChildAlive(divY, test[1]);
    checkParentAndChildAlive(divZ, test[2]);

    debug("=== After clearing innerHTML ===");
    rootDiv.innerHTML = testHtml;
    divX = document.getElementById(test[0]);
    divY = document.getElementById(test[1]);
    divZ = document.getElementById(test[2]);
    rootDiv.innerHTML = "";
    checkParentAndChildAlive(divX, test[0]);
    checkParentAndChildAlive(divY, test[1]);
    checkParentAndChildAlive(divZ, test[2]);

    debug("=== After clearing innerHTML and divX ===");
    rootDiv.innerHTML = testHtml;
    divX = document.getElementById(test[0]);
    divY = document.getElementById(test[1]);
    divZ = document.getElementById(test[2]);
    rootDiv.innerHTML = "";
    divX = null;
    gc();
    checkParentAndChildAlive(divY, test[1]);
    checkParentAndChildAlive(divZ, test[2]);

    debug("=== After clearing innerHTML, divX and divY ===");
    rootDiv.innerHTML = testHtml;
    divX = document.getElementById(test[0]);
    divY = document.getElementById(test[1]);
    divZ = document.getElementById(test[2]);
    rootDiv.innerHTML = "";
    divX = null;
    divY = null;
    gc();
    checkParentAndChildAlive(divZ, test[2]);

    debug("=== After clearing innerHTML, divX, divY and divZ ===");
    rootDiv.innerHTML = testHtml;
    divX = document.getElementById(test[0]);
    divY = document.getElementById(test[1]);
    divZ = document.getElementById(test[2]);

    rootDiv.innerHTML = "";
    divX = null;
    divY = null;
    divZ = null;
});

function checkParentAndChildAlive(div, name) {
    globalDiv = div;
    shouldBeEqualToString('globalDiv.id', name);
    shouldBeEqualToString('globalDiv.parentNode.id', name + "-parent");
    shouldBeEqualToString('globalDiv.firstChild.id', name + "-child");
    globalDiv = null;
    gc();
}

var successfullyParsed = true;
</script>
</body>
</html>
